package com.controller;

import com.dao.UserDao;
import com.entity.User;
import com.entity.UserFile;
import com.entity.UserMetadata;
import com.service.SystemOperator;
import com.service.UserFileOperator;
import com.service.UserOperator;
import com.util.CookieUtil;
import com.util.LocalInformation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import static com.util.GlobalArgs.UserCookie_key;


/**
 * Created by wlc on 2017/11/13.
 */

/**
 * 映射系统登录注册注销部分
 */
@Controller
@RequestMapping(value = "/home",produces = "text/html;charset=UTF-8")
public class HomeController {
    //映射home.jsp
    @RequestMapping(value = "/home",produces = "text/html;charset=UTF-8")
    public ModelAndView home(){
        ModelAndView modelAndView = new ModelAndView();

        ArrayList<UserFile> announcementFilesInfo = UserFileOperator.getAllFilesInfo("公告");

        modelAndView.addObject("dataType_dataNum", SystemOperator.getDataNum());
        modelAndView.addObject("announcementFilesInfo", announcementFilesInfo);
        modelAndView.setViewName("home");

        return modelAndView;
    }

    //映射login.jsp
    @RequestMapping(value = "/login",produces = "text/html;charset=UTF-8")
    public ModelAndView login(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();

        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        if (user_cookie_value != null && UserMetadata.OnlineUserId.containsKey(user_cookie_value)){
            UserMetadata.OnlineUserOperatorTime.replace(user_cookie_value, LocalInformation.getTimeID());

            ArrayList<UserFile> bulletinFilesInfo = UserFileOperator.getAllFilesInfo("通知");
            ArrayList<UserFile> computeFilesInfo = UserFileOperator.getAllFilesInfo("计算");
            ArrayList<UserFile> experimentFilesInfo = UserFileOperator.getAllFilesInfo("实验");
            ArrayList<UserFile> followFilesInfo = UserFileOperator.getAllFilesInfo("随访");

            modelAndView.addObject("bulletinFilesInfo",bulletinFilesInfo);
            modelAndView.addObject("computeFilesInfo",computeFilesInfo);
            modelAndView.addObject("experimentFilesInfo",experimentFilesInfo);
            modelAndView.addObject("followFilesInfo",followFilesInfo);
            modelAndView.setViewName("SystemHome");
        }else {
            modelAndView.setViewName("login");
        }

        return modelAndView;
    }

    //映射登录-判断用户名密码是否正确
    @RequestMapping(value = "/login_userexist",produces = "text/html;charset=UTF-8")
    public void login_userexist(HttpServletResponse response,
                                @RequestParam(value = "username")String username,
                                @RequestParam(value = "password")String password) throws IOException{
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        String user_infor = UserDao.isUserExist(user);

        PrintWriter out= null;
        out = response.getWriter();
        if (!user_infor.equals("null")){
            out.print(user_infor);
        }else {
            out.print("false");
        }
        out.flush();
        out.close();
    }

    //映射登录-进入系统
    @RequestMapping(value = "/login_system",produces = "text/html;charset=UTF-8")
    public ModelAndView login_system(HttpServletResponse response,HttpServletRequest request) throws Exception{
        ModelAndView modelAndView = new ModelAndView();
        String user_infor = request.getParameter("user_login_info");
        String projectPath = request.getSession().getServletContext().getRealPath("/");

        String cookie_value = UserOperator.log_in(user_infor,projectPath);
        Cookie cookie = new Cookie(UserCookie_key,cookie_value);
        cookie.setPath("/");  //设置作用到所有映射关系
        response.addCookie(cookie);

        ArrayList<UserFile> bulletinFilesInfo = UserFileOperator.getAllFilesInfo("通知");
        ArrayList<UserFile> computeFilesInfo = UserFileOperator.getAllFilesInfo("计算");
        ArrayList<UserFile> experimentFilesInfo = UserFileOperator.getAllFilesInfo("实验");
        ArrayList<UserFile> followFilesInfo = UserFileOperator.getAllFilesInfo("随访");

        modelAndView.addObject("bulletinFilesInfo",bulletinFilesInfo);
        modelAndView.addObject("computeFilesInfo",computeFilesInfo);
        modelAndView.addObject("experimentFilesInfo",experimentFilesInfo);
        modelAndView.addObject("followFilesInfo",followFilesInfo);

        modelAndView.setViewName("SystemHome");
        return modelAndView;
    }

    //映射register.jsp
    @RequestMapping(value = "/register",produces = "text/html;charset=UTF-8")
    public ModelAndView register(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("register");

        return modelAndView;
    }

    //映射注册-判断用户是否存在
    @RequestMapping(value = "/register_userexist",produces = "text/html;charset=UTF-8")
    public void register_userexist(HttpServletRequest request,HttpServletResponse response) throws IOException{
        PrintWriter out= null;
        out = response.getWriter();
        out.print(UserDao.isUserRegister(request.getParameter("username")));
        out.flush();
        out.close();
    }

    //映射注册-添加新用户
    @RequestMapping(value = "/register_adduser",produces = "text/html;charset=UTF-8")
    public String register_adduser(User user){
        UserDao.addNewUser(user);
        return "login";
    }

    //映射注销
    @RequestMapping(value = "/logout",produces = "text/html;charset=UTF-8")
    public ModelAndView logout(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();

        Cookie user_cookie = CookieUtil.getUserCookie(request);
        String user_info_key = user_cookie.getValue();
        UserOperator.log_out(user_info_key);

        ArrayList<UserFile> announcementFilesInfo = UserFileOperator.getAllFilesInfo("公告");

        modelAndView.addObject("dataType_dataNum", SystemOperator.getDataNum());
        modelAndView.addObject("announcementFilesInfo", announcementFilesInfo);

        modelAndView.setViewName("home");
        return modelAndView;
    }
}